<?php

namespace app\admin\controller\booking;

use app\common\controller\Backend;
use addons\booking\model\OrderCalendar;
use addons\booking\library\Service;
use app\admin\model\booking\House;
use fast\Date;

/**
 * 日历价格管理
 *
 * @icon fa fa-circle-o
 */
class Calendar extends Backend
{

    /**
     * Calendar模型对象
     * @var \app\admin\model\booking\Calendar
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\booking\Calendar;
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        $house_id = $this->request->param('house_id');
        $year = $this->request->param('year');
        $month = $this->request->param('month');
        //不传，默认当前月的时间戳
        if (!$year) {
            $year = date('Y');
        }
        if (!$month) {
            $month = date('m');
        }

        //月的第一天和下个月第一天时间戳
        $start_time = $month_first = strtotime("{$year}-{$month}-01");

        //本月的第一天是周几
        $week = date('w', $month_first);
        if ($week > 0) { //大于0，需要补齐上个月的日历。
            $start_time = Date::unixtime('day', -$week, 'begin', $year, $month, 1);
        }
        //计算下个月
        if ($month == 12) {
            $year += 1;
            $month = 1;
            $nex_month_first = strtotime("{$year}-01-01");
        } else {
            $month += 1;
            $nex_month_first = strtotime("{$year}-{$month}-01");
        }
        $end_time = $nex_month_first;

        //下个月的第一天是周几
        $week = date('w', $nex_month_first);
        if ((7 - $week) > 0) { //大于0，需要补齐上个月的日历。
            $end_time = Date::unixtime('day', 7 - $week, 'begin', $year, $month, 1);
        }

        $list = $this->model
            ->where('house_id', $house_id)
            ->where('day_time', '>=', $start_time)
            ->where('day_time', '<', $end_time)
            ->select();

        $orderCalendarList = OrderCalendar::field('id,house_id,reserve_time')
            ->where('house_id', $house_id)
            ->where('status','normal')
            ->where('reserve_time', '>=', $start_time)
            ->where('reserve_time', '<', $end_time)
            ->select();

        $data = [];
        foreach ($list as $item) {
            $t = date('m-d', $item['day_time']);
            $data[$t] = $item['price'];
        }

        $newData = [];
        foreach ($orderCalendarList as $item) {
            $ot = date('m-d', $item['reserve_time']);
            $newData[$ot] = $item;
        }
        //查找数据
        if ($this->request->isAjax()) {
            $this->success('', '', ['list' => $data, 'orderCalendar' => $newData]);
        }
        $holidays = Service::getHolidays();
        $house = House::get($house_id);
        $this->assignconfig('holidays', $holidays);
        $this->assignconfig('house', $house);
        $this->assignconfig('house_id', $house_id);
        return $this->view->fetch();
    }


    /**
     * 添加
     */
    public function add()
    {
        $day_time = $this->request->post('day_time');
        $house_id = $this->request->post('house_id/d');
        $price = (int)$this->request->post('price/d');
        if (empty($day_time) || empty($house_id)) {
            $this->error('参数错误！');
        }
        if ($price < 0) {
            $this->error('价格不能小于0！');
        }
        if (!checkDateFormat($day_time)) {
            $this->error('日期格式错误！');
        }
        $times = strtotime($day_time);
        //查找已有的数据，修改
        $calendar_data = $this->model->where('house_id', $house_id)->where('day_time', $times)->find();
        if ($calendar_data) {
            $calendar_data->save(['price' => $price]);
        } else {
            $this->model->save([
                'house_id' => $house_id,
                'day_time' => $times,
                'price' => $price
            ]);
        }
        $this->success('保存成功');
    }
}
